(function($){
    // Envoi au serveur

    $('#login_form').submit(function(event){
        event.preventDefault();
        socket.emit('cl_login', $('#username').val(),  $('#password').val());
    });

    $('#registration_link').click(function(event){
        event.preventDefault();
        var registration = $('#registration');

        if (!registration.is(":visible")) { registration.show(500); }
        else { registration.hide(500); }
    });

    $('#registration_form').submit(function(event){
        event.preventDefault();
        socket.emit('cl_registration', $('#username_reg').val(), $('#password_reg').val());
    });


    // Réception des events du serveur

    //USER CONNECT EVENT
//    socket.on('sv_logged', function(playerHero){
//    })

    //NEW USER IN GAME EVENT
    socket.on('sv_newusr', function(user){
        $('#players_list').append('<li id="usr_' + user + '">' + user + '</li>');
    })

    //REMOVE USER EVENT
    socket.on('sv_delusr', function(user){
        $('#usr_' + user).fadeOut();
        $('#usr_' + user).remove();
    })

    //USER REGISTERED EVENT
    socket.on('sv_registered', function(){
        $('#registration').hide(500);
    })

    socket.on('sv_game_loaded', function(map, heros, heroName) {
        //On charge d'abord la map puis les datas des heros
        hero.name = heroName;
        loadMap(map);
        loadHerosData(heros);

        $('#login').fadeOut();

        setCanvas();
        setUI();
        setInterval(updateCropsTimers, 2000);
        draw();

        canvas.style.visibility = 'visible';
        ui_top.style.visibility = 'visible';
        ui_bottom.style.visibility = 'visible';
    })

    socket.on('sv_move_event', function(sv_hero, sv_steps) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

        // .id et non ._id car on recois un sv_hero sous forme client (simple object)
        var movingHero = findHeroById(sv_hero.id);
        if(movingHero == null)
            console.log('enemy hero introuvable (move event) avec id '+sv_hero.id);
        else {
//            console.log('hero enemy move '+sv_hero.name);
            movingHero.path.simpleSteps = sv_steps;
            movingHero.moveEnemy();
        }
    })

    socket.on('sv_tile_update', function(sv_tile) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

        reloadTile(sv_tile);
    })

    socket.on('sv_crop_updated', function(sv_crop) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

//        console.log('crop updated ; ');
//        console.log(sv_crop);

        reloadCrop(sv_crop);
    });

    socket.on('sv_crop_created', function(sv_crop) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

//        console.log('crop created : ');
//        console.log(sv_crop);

        newCrop(sv_crop);
    });

    socket.on('sv_crop_deleted', function(sv_cropPos) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

        deleteCropAtPos(sv_cropPos);
    })

    socket.on('sv_building_updated', function(sv_building) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

        console.log('building updated ; ');
        console.log(sv_building);

//        reloadBuilding(sv_building);
    });

    socket.on('sv_building_created', function(sv_building) {
        if(!hero.connected) {
            console.log('aborted event : client not connected');
            return;
        }

//        console.log('building created : ');
//        console.log(sv_building);

        newBuilding(sv_building);
    });



})(jQuery);